package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * 公告信息Controller
 * 
 * @author ruoyi
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/stapp/food")
public class StFoodAppController extends BaseController
{
    @Autowired
    private IStFoodService stFoodService;
    @Autowired
    private SysLoginService appsysloginService;
    @Autowired
    private IStClassService stClassService;
    @Autowired
    private IStFoodrawService stFoodrawService;
    @Autowired
    private IStCommentService stCommentService;
    //获取食堂简介
    @PostMapping("classlist")
    HashMap<Object, Object> classlist(@RequestBody  StClass stClass) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);

        List<StClass> StClasslist = stClassService.selectStClassList(stClass);
        aa.put("requestdata",StClasslist);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    //获取食堂简介
    @PostMapping("foodlist")
    HashMap<Object, Object> foodlist(@RequestBody StFood stFood) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        List<StFood> StFoodlist = stFoodService.selectStFoodList(stFood);
        aa.put("requestdata",StFoodlist);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("foodrawlist")
    HashMap<Object, Object> foodrawlist(@RequestBody StFoodraw stFoodraw) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        List<StFoodraw> StFoodrawlist = stFoodrawService.selectStFoodrawList(stFoodraw);
        aa.put("requestdata",StFoodrawlist);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("commentlist")
    HashMap<Object, Object> commentlist(@RequestBody StComment stComment) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        List<StComment> stCommentlist = stCommentService.selectStCommentList(stComment);
        aa.put("requestdata",stCommentlist);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("commenting")
    HashMap<Object, Object> commenting(@RequestBody StComment stComment) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        stComment.setId(newsNo);
        stCommentService.insertStComment(stComment);
        aa.put("requestdata","");
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("/loginwxphone")
    public AjaxResult loginwxphone(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        String s = HttpUtils.sendPost("https://api.weixin.qq.com/cgi-bin/stable_token", "{\"appid\": \"wx38764e5c5a0046a1\",\"secret\": \"b0976852efd5bbae1ce688efd11d1aee\",\"grant_type\": \"client_credential\"}");
     JSONObject jsonObject = JSONObject.parseObject(s);
     String accessToken = jsonObject.getString("access_token");

       String requsetParam = "{\"code\": \""+loginBody.getPhone()+"\"}";
       System.out.println(">>>>>>>>>>>>>>>>>>>"+requsetParam);
        String s1 = HttpUtils.sendPost("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken, requsetParam);
        JSONObject jsonObject1 = JSONObject.parseObject(s1);
        JSONObject accessToken1 = jsonObject1.getJSONObject("phone_info");
        String tel = accessToken1.getString("phoneNumber");
        // 生成令牌
        String token = appsysloginService.loginphone(tel);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

}
